package com.br4mmie.asciistory;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.br4mmie.asciistory.characters.NPC;
import com.br4mmie.asciistory.characters.Player;
import com.br4mmie.asciistory.map.Map;
import com.br4mmie.asciistory.map.MapLoader;
import com.br4mmie.asciistory.map.MapObject;
import com.br4mmie.asciistory.map.PlayerAvatar;
import com.br4mmie.asciistory.mission.Inventory;
import com.br4mmie.asciistory.mission.Mission;
import com.br4mmie.asciistory.ui.OptionWindow;


public class ProfileManager 
{
	private String profileName = "";
	private GameLoop gameLoop;
	
	public ProfileManager(GameLoop gameLoop)
	{
		this.gameLoop = gameLoop;
	}
	
	public void setGameLoop(GameLoop gameLoop)
	{
		this.gameLoop = gameLoop;
	}
	
	public void loadGame(String filename, boolean loadPlayer) throws IOException
	{
		filename = "Saves/"+filename;
		
		BufferedReader reader = new BufferedReader(new FileReader(FileGetter.getFilename(filename)));
		MapLoader loader = new MapLoader("Data/Maps/map1.map");
		gameLoop.setMap(loader.createMap());
		
		while (true)
		{
			String line = reader.readLine();
			if (line == null) break;
			
			if (line.startsWith("map"))
			{
				gameLoop.setMap(Map.restore(reader));
				
			} else if (line.startsWith("player"))
			{
				gameLoop.setPlayer(Player.restore(reader));
			} else if (line.startsWith("inventory"))
			{
				gameLoop.getPlayer().restoreInventory(Inventory.restore(reader));
			} else if (line.startsWith("avatar"))
			{
				gameLoop.setAvatar(PlayerAvatar.restore(reader, gameLoop.getMap()));;
			} else if (line.startsWith("mission"))
			{
				Mission.restore(reader, line.split(" ")[1], gameLoop.getPlayer());
			} else if (line.startsWith("npc"))
			{
				String name = reader.readLine().substring(5);
				String missionName = reader.readLine().substring(6);
				reader.readLine();
				for (MapObject mo: gameLoop.getMap().getAllObjects())
				{
					if (mo instanceof NPC)
					{
						if (((NPC)mo).getName().equals(name))
						{							
							//search for the mission in player, because we want the same mission to be the same object
							for (Mission m: gameLoop.getPlayer().getMissions())
							{
								if (m.getName().equals(missionName))
								{
									((NPC)mo).restoreMission(m);
								}
							}
							//if not found, the npc will have it's default mission loaded already
						}
					}
				}
			}
		}
	}
	public void saveGame(String filename, boolean saveplayer) throws IOException
	{
		filename = "Saves/"+filename;
		BufferedWriter writer = new BufferedWriter(new FileWriter(FileGetter.getFilename(filename)));
		
		gameLoop.getMap().save(writer);
		if (saveplayer)
		{
			gameLoop.getPlayer().save(writer);
			gameLoop.getPlayer().getInventory().save(writer);
			gameLoop.getAvatar().save(writer);
			for (Mission mission: gameLoop.getPlayer().getMissions())
			{
				mission.save(writer);
			}
		}
		for (MapObject mo: gameLoop.getMap().getAllObjects())
		{
			if (mo instanceof NPC)
			{
				((NPC)mo).save(writer);
			}
		}
		
		writer.close();
	}
	
	public String getProfileName()
	{
		return profileName;
	}
	public void setProfilename(String profileName)
	{
		this.profileName = profileName;
	}

	private static boolean loadgame;
	protected static boolean getLoadgame() {return loadgame;}
	public void requestProfile(boolean loadgame)
	{
		ProfileManager.loadgame = loadgame;
		OptionWindow.SelectionListener profileListener = new OptionWindow.SelectionListener(){

			@Override
			public void select(int option) 
			{
				switch (option)
				{
				case 1: profileName="Profile_Slot_1"; break;
				case 2: profileName="Profile_Slot_2"; break;
				case 3: profileName="Profile_Slot_3"; break;
				case 4: profileName="Profile_Slot_4"; break;
				case 5: profileName="Profile_Slot_5"; break;
				case 6: profileName="Profile_Slot_6"; break;
				case 7: profileName="Profile_Slot_7"; break;
				case 8: profileName="Profile_Slot_8"; break;
				case 9: profileName="Profile_Slot_9"; break;
				case 10: profileName="Profile_Slot_10"; break;
				}
				
				try {
				if (ProfileManager.getLoadgame()) 	{loadGame(profileName + ".root.savegame", true);}
				else		      					{gameLoop.startNewGame(profileName);}
				} catch (Exception e) {e.printStackTrace();}
			}
		};
		
		OptionWindow ow = new OptionWindow(0, 0, gameLoop.getScreenWidth(), gameLoop.getScreenHeight(), "Choose a save slot", profileListener);
		ow.addOption("Slot 1");
		ow.addOption("Slot 2");
		ow.addOption("Slot 3");
		ow.addOption("Slot 4");
		ow.addOption("Slot 5");
		ow.addOption("Slot 6");
		ow.addOption("Slot 7");
		ow.addOption("Slot 8");
		ow.addOption("Slot 9");
		ow.addOption("Slot 10");
	}
}
